package com.dating.libs.web.services.registration;

import java.util.ArrayList;
import java.util.List;

import org.ksoap2.HeaderProperty;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.util.Log;

import com.dating.activity.Dating;

public final class RegistrationService {
	// return FAIL
	public java.lang.String getHello() throws Exception {
		String methodName = "getHello";
		SoapObject _client = new SoapObject(Configuration.wsNameSpace,
				methodName);
		SoapSerializationEnvelope _envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		addMapping(_envelope); // IMPORTANT!!!
		_envelope.bodyOut = _client;
		HttpTransportSE _ht = new HttpTransportSE(Configuration.getWsUrl());
		_ht.call("", _envelope, setHeader());
		SoapObject body = (SoapObject) _envelope.bodyIn;
		return String.valueOf(body.getProperty("return"));
	}

	// ham 1
	public RegistrationCredential requestRegistryPermission(
			java.lang.String clientID, java.lang.String type) throws Exception {
		SoapObject _client = new SoapObject(Configuration.wsNameSpace,
				"requestRegistryPermission");
		_client.addProperty("clientID", clientID);
		_client.addProperty("type", type);
		SoapSerializationEnvelope _envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		addMapping(_envelope); // IMPORTANT!!!
		_envelope.bodyOut = _client;
		HttpTransportSE _ht = new HttpTransportSE(Configuration.getWsUrl());
		_ht.call(Configuration.wsNameSpace, _envelope);
		SoapObject _ret = (SoapObject) _envelope.bodyIn;
		_ret = (SoapObject) _ret.getProperty(0);
		int _len = _ret.getPropertyCount();
		RegistrationCredential _returned = new RegistrationCredential();
		for (int _i = 0; _i < _len; _i++) {
			_returned.setProperty(_i, _ret.getProperty(_i));
		}
		Log.e("token", _returned.getAutheToken(""));
		Log.e("uid", _returned.getRid(""));
		return _returned;
	}

	public boolean registryBasicInformation(java.lang.String name,
			java.lang.String email, java.lang.String passMD5, boolean gender,
			java.lang.String birthday, java.lang.String address)
			throws Exception {
		SoapObject _client = new SoapObject(Configuration.wsNameSpace,
				"registryBasicInformation");
		_client.addProperty("name", name);
		_client.addProperty("email", email);
		_client.addProperty("passMD5", passMD5);
		_client.addProperty("gender", gender + "");
		_client.addProperty("birthday", birthday);
		_client.addProperty("address", address);
		SoapSerializationEnvelope _envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		addMapping(_envelope); // IMPORTANT!!!
		_envelope.bodyOut = _client;
		HttpTransportSE _ht = new HttpTransportSE(Configuration.getWsUrl());
		_ht.call("", _envelope, setHeader());
		return _envelope.getResponse().toString().equals("true");
	}

	public boolean updateFBSocial(java.lang.String jsonString,
			java.lang.String type)
			throws Exception {
		SoapObject _client = new SoapObject(Configuration.wsNameSpace, "updateFBSocial");
		_client.addProperty("jsonString", jsonString);
		_client.addProperty("type", type);
		SoapSerializationEnvelope _envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		addMapping(_envelope); // IMPORTANT!!!
		_envelope.bodyOut = _client;
		HttpTransportSE _ht = new HttpTransportSE(Configuration.getWsUrl());
		_ht.call("", _envelope, setHeader());
		return _envelope.getResponse().toString().equals("true");
	}

	public boolean updateFBAccountInformation(java.lang.String FBAccessToken,
			java.lang.String expireTime) throws Exception {
		SoapObject _client = new SoapObject(Configuration.wsNameSpace, "updateFBAccountInformation");
		_client.addProperty("FBAccessToken", FBAccessToken);
		_client.addProperty("expireTime", expireTime);
		SoapSerializationEnvelope _envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		addMapping(_envelope); // IMPORTANT!!!
		_envelope.bodyOut = _client;
		HttpTransportSE _ht = new HttpTransportSE(Configuration.getWsUrl());
		_ht.call("", _envelope, setHeader());
		return _envelope.getResponse().toString().equals("true");
	}

	public boolean complete() throws Exception {
		SoapObject _client = new SoapObject(Configuration.wsNameSpace,
				"complete");
		SoapSerializationEnvelope _envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		addMapping(_envelope); // IMPORTANT!!!
		_envelope.bodyOut = _client;
		HttpTransportSE _ht = new HttpTransportSE(Configuration.getWsUrl());
		_ht.call("", _envelope, setHeader());
		return _envelope.getResponse().toString().equals("true");
	}

	/************************************ CONFIG ***********************************/
	private void addMapping(SoapSerializationEnvelope envelope) {
		envelope.addMapping(Configuration.wsNameSpace,
				"registrationCredential", new RegistrationCredential()
						.getClass()); // IMPORTANT!!!
	}

	private List<HeaderProperty> setHeader() {
		List<HeaderProperty> headerList = new ArrayList<HeaderProperty>();

		headerList.add(new HeaderProperty("SessionID", Dating.ME.UID));
		headerList.add(new HeaderProperty("AuthenticateToken", Dating.ME.AUTH));

		return headerList;
	}
}
